/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Product;
import persistence.PersistenceInterface;
import persistence.PersistenceFactory;

/**
 *
 * @author isen
 */
public class CreateProductServlet extends CoolServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PersistenceInterface persistenceManager = PersistenceFactory.getInstance(persistenceMechanism);
        Product product = generateProductFromRequest(request);

        if (product != null && persistenceManager.createProduct(product)) {
            gotoURL(successForm, request, response);
        } else {
            request.setAttribute("resultado", "Error to create the product, please try again to fill in th form");
            request.getRequestDispatcher("forms/CreateProductForm.jsp").forward(request, response);
        }
    }

    Product generateProductFromRequest(HttpServletRequest request) {

        PersistenceInterface persistenceManager = PersistenceFactory.getInstance(persistenceMechanism);
        Product product;
        String productName = request.getParameter("ProductName");
        String stockuser = request.getParameter("Stock");
        String shortDescription = request.getParameter("ShortDescription");
        String longDescription = request.getParameter("LongDescription");
        String precioUser = request.getParameter("ProductPrice").replaceAll(",", ".");
        double precio;
        int stock;

        try {
           
            try{
                stock = Integer.parseInt(stockuser);
                precio = Double.parseDouble(precioUser);
            }catch(NumberFormatException ex){
                request.setAttribute("resultado", "The price and stock should be number");
                return null;
            }
            if(!validateForm(request)) {
                request.setAttribute("resultado", "You have to fill in ALL the fieds to create a product ");
                return null;
            }if(!validateName(productName)) {
                request.setAttribute("resultado", "The name has to be a word with only letter");
                return null;
            }if (persistenceManager.productExists(productName)) {
                request.setAttribute("resultado", "The product you want to create already exists");
                return null;

            }else{
                product = new Product(productName, stock, shortDescription, longDescription, precio);
                return product;
            }
        } catch (Exception e) {
            request.setAttribute("resultado", "Error, you have to fill in all the fieds to create a product");
        }
        return null;
    }

    public boolean validateForm(HttpServletRequest request) {
        if (!"".equals(request.getParameter("ProductName")) && (!"".equals(request.getParameter("Stock")))
                && !"".equals(request.getParameter("ShortDescription")) 
                && !"".equals(request.getParameter("LongDescription")) && 
                !"".equals(request.getParameter("ProductPrice")))  {
            return true;
        }
        return false;
    }
    
    private boolean validateName(String name) {
        String namePattern = "[a-zA-Z]+";
        return name.matches(namePattern);
    }
    
}
